<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        span {
            color: #ccc;
        }

        .right {
            color: green;
        }

        .arrow {
            color: red;
        }
    </style>
</head>

<body>
    <input type="" class="uname"> <span>请输入用户名</span>
    <script>
        var rg = /^[a-zA-Z0-9_-]{6,16}$/
        var uname = document.querySelector('.uname')
        var span = document.querySelector('span')
        uname.onblur = function () {
            if (rg.test(this.value)) {
                console.log('yes');
                span.className = 'right'
                span.innerHTML = '用户名输入正确'

            } else {
                console.log('no');
                span.className = 'arrow'
                span.innerHTML = '用户名格式错误'

            }
        }

    </script>
</body>

</html>